package com.controller.front_controller;


import com.mypg.News;
import com.service.NewsService;
import com.service.UNewsReadService;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("/front_news")
@Controller
public class FrontNewsController {

    @Autowired
    NewsService newsService;

    @Autowired
    UserService userService;

    @Autowired
    UNewsReadService uNewsReadService;

    @RequestMapping("/news_init_index.do")
    @ResponseBody
    public List<News> doNews_init_index(){
        return newsService.selectAllNews();
    }

    @RequestMapping("/news_detail_init.do")
    @ResponseBody
    public News doNews_detail_init(int news_id){
        return newsService.selectNewsByNews_id(news_id);
    }

    @RequestMapping("/news_relative.do")
    @ResponseBody
    public List<News> doNews_relative(String news_class,int news_id){
        return newsService.selectRelativeNews(news_class,news_id);
    }

    @RequestMapping("/news_read_init.do")
    @ResponseBody
    public void donNews_read_init(int news_id, HttpSession session){
        try {
            String user_name = (String) session.getAttribute("user_name");
            int user_id=userService.selectUserByUser_name(user_name).getUser_id();
            int num=uNewsReadService.selectCountByUser_idAndNews_id(user_id,news_id);
            if(num==0){
                uNewsReadService.insertUNewsRead(user_id,news_id);
                newsService.updateNews_readByNews_id(news_id);
            }
        }catch (Exception e){

        }
    }

}
